//*********************************************************************
//		Copyright (c) 2003 - 2008 Microsoft Corporation.
//      This code is provided under Microsoft Public License, which can be found at
//      http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx 
//
// @File: CollectorError.cs
//
// Purpose: Error codes and classes
//
//
//
// Notes: 
//
//
//	
// History: 
//     @Version: V2.1
//
//     07/14/08 PGV 2.1.001.0 Starting V2.1 (based on 2.0.030.0 code)
//
//
// @EndHeader@
//*********************************************************************

using System;
using System.Management;

namespace H2V2
{
    public enum RunErrorClass
    {
        // mapped to mgr_run_error_class table
        Unspecified = 0,
        Server = 1,
        SqlServer = 2,
        Wmi = 3,
        EventLog = 4,
        File = 5,
        Database = 6,
        Configuration = 7,
        Registry = 8,
        PerfProvider = 9,
        Exporter = 10,
        FileSystem = 11
    }

    public enum CollectorError
    {
        // error IDs backward compatible with SQLH2 v.2.0

        UnexpectedException = 1,
        AccessDenied = 2,
        SecurityError = 3, // A security package specific error occurred
        WmiInvalidClass = 4, // InvalidClass (non-critical; most likely provider is not installed)
        WmiProviderLoadFailure = 5, // (same as 4)

        // Detector
        DetectorNoInstancesFound = 50, // No SQL Server instances registered on the box



        // Internal errors
        UnknownStoreType = 100, 

        //  Stor errors (SqlException-related)
        StorCannotConnect = 101, // Something wrong with connection: 
                // inability to open a connection (wrong connection string | server is not accessible) | 
        StorDatareaderFailed = 110, // exception executing simple query (like select serverproperty...) - datareader.read failed 
        
        // Sql syntax error (no object exists, wrong parameter name, ...) 
        StorSyntaxErrorReaderSP = 102, // sql syntax error (sp - ExecuteReader error)
        StorSyntaxErrorReaderSelect = 103, // sql syntax error (select)
        StorSyntaxErrorNonQuery = 104, // sql syntax error (insert/delete/update - ExecuteNonQuery)    
        StorSyntaxErrorScalar = 105, // sql syntax error (ExecuteScalar)

        // Unexpected results
        StorUnexpectedResult = 200, 
        StorNoResult = 201, // no result (no records returned)
  
        // Returned errors (from procedures)
        StorSpError = 300, // sp returned "user" error

        // Unit errors
        UnitAlreadyOpened = 401, // Attempt to open a Unit, when another one is already open
        MissingOpenUnit = 402, // Operation requested requires open Unit, but there is no open Unit.

        //  Registry
        CannotFindRegistryKey = 501, // ex: Can't find instance key, PerfProvider key
        CannotFindValue = 502, // can't find errorlog path ('-e' parameter)
        RegistryInvalidArgument = 510,
        RegistryUnauthorizedAccessException = 511, 
        RegistrySecurityException = 512, 
        RegistrySecurityExceptionAccessingSql = 513, // Failure to access "SOFTWARE\\Microsoft\\Microsoft SQL Server" (SecurityException type 2)
        RegistryPathNotFound = 521, // IOException (path not found) - RemoteResitry service may be OFF
  

        //  Files
        FileCannotFindFile = 601, 
        FileCannotFindPath = 602,
  
        // SQL  Collection
        SqlTimeout = 701, // Timeout 
        SqlUnexpectedException = 702, // any other exception not handled lower
  
        // SQL Objects
        SqlDbInaccessible = 801, 
        SqlMissingPerfCounter = 802, 
        SqlDbNotOnline = 803, 


        //  (log corruption/ irregular events deletion)
        LogPidDoesntMatchCap = 901, // the first 17177 doesn't match pid of the cap event and 
                              // references to earlier time than the cap.TimeGenerated 
                              // (regardless of History gaps)
        LogTimeDoesntMatch = 902, // the first 17177 matches the cap's pid but has a different timestamp (no history gap)
        LogPidDoesntMatch = 903, // hole in sequence  - 17177 doesn't match the previous pid (no history gap) 
        LogReferencedTimeChanged = 904, // referenced time changed (same pid; no history gap)
        LogRepeatingSystemEvents = 905, 
        LogReferencedTimeDoesntMatch = 906, // the first 17177 matches pid of the cap event, but references to a different time 
        LogUnexpectedEventFormat = 910,
        LogTraceStopMissed = 920, // trace stop missed (repeating event 19030 for the same trace_id)
  
        CannotFindEventLog = 951, 
        CannotAccessEventLog = 952,
        CannotReadLogEntry = 953, 
    
        CannotPositionLog = 961, // There is no gap in history, but the previous last event is out of bounds
        LogPositionDoesntMatch = 962, // Log positioning algorithm resulted in non-matching event (index or time)

        // PerfProvider
        PerfProviderBcpFailure = 1001, // bcp failed (any reason)
        PerfProviderLocked = 1002, // Unable to obtain Lock to load perf data
        PerfProviderPrepareTimeout = 1003, // Subsequent timeouts received preparing Perf data
        PerfProviderInsertTimeout = 1004, // Subsequent timeouts received inserting Perf data
        PerfProviderCannotClean = 1005, // Unable to clean temp tables
        ExternalCannotFindBcp = 1006, // Could not find bcp
        PerfProviderBcpCopyInFailure = 1007, 

        PerfProviderUnableToContinue = 1011, 
        PerfProviderErrorsEncountered = 1012,

  
        // Exporter
        ExporterProcessFailure = 1101,
        ExportedNoDiskSpace = 1102, // Init failure (no disk space)
        ExporterCannotInit = 1103, // Init Failure (Unable to create a temporary directory)
        ExporterCannotClean = 1105, // Unable to clean temporary files
    }

	public class CollectorErrorException : Exception
	{
		public CollectorError m_Error;
		public int m_auxError = 0;
		public ManagementStatus m_wmiStatus;
		public bool m_IsWMI = false;

        public CollectorErrorException (CollectorError a_Error, int a_auxError, string a_object)
            : base (a_object) 
		{
			m_Error = a_Error;
			m_auxError = a_auxError;
		}

        public CollectorErrorException (CollectorError a_Error, ManagementStatus a_status, string a_object)
            : base (a_object)
		{
			m_Error = a_Error;
			m_wmiStatus = a_status;
			m_IsWMI = true;
		}
	}

    public class CriticalCollectorErrorException : Exception
    {
        public CollectorError m_Error;

        public CriticalCollectorErrorException (CollectorError error, string msg)
            : base (msg)
        {
            m_Error = error;
        }

        public CriticalCollectorErrorException (CollectorError error, string msg, Exception innerException)
            : base (msg, innerException)
        {
            m_Error = error;
        }
    }

}
